//
// Created by benxb on 2021/12/11.
//

#include <gtest/gtest.h>
#include <mem/data_table_manager.h>
#include <access/database.h>
#include <catelog/table.h>
#include <vector>
#include <catelog/column.h>
#include <cstdio>
#include <memory>
#include <iostream>

TEST(DatabaseTest, CreateDataset) {
    benxdb::Database db;
    bool res = db.CreateDataset("test", benxdb::DATASET_XYZ_INTENSITY);
    EXPECT_EQ(res, true);
}

TEST(DatabaseTest, AppendPointCloud) {
    benxdb::Database db;
    bool res = db.CreateDataset("test", benxdb::DATASET_XYZ_INTENSITY);
    EXPECT_EQ(res, true);
}

TEST(DatabaseTest, GetAllDatasetsNames) {
    benxdb::Database db;
    std::vector<std::string> names;
    db.AllDatasets(names);
    for (auto name : names) {
        std::cout << name << std::endl;
    }
}


TEST(DatabaseTest, AppendRecord) {
    std::vector<benxdb::Column> columns;
    columns.emplace_back("x", benxdb::FLOAT);
    columns.emplace_back("y", benxdb::FLOAT);
    columns.emplace_back("z", benxdb::FLOAT);
    columns.emplace_back("intensity", benxdb::FLOAT);
    benxdb::Database db;
    db.CreateTable(std::make_shared<benxdb::Table>("test", columns));

    benxdb::PointCloud<benxdb::PointXYZI> point_cloud;
    for (int i = 0; i < 7000089; i++) {
        benxdb::PointXYZI point{};
        point.x = 1.023f;
        point.y = 1.023f;
        point.z = 1.023f;
        point.intensity = 1.023f;
        point_cloud.Append(point);
    }
    std::cout << point_cloud.Size() << std::endl;
    db.AppendPointCloud("test", point_cloud);
}

TEST(DatabaseTest, AppendRandRecord) {
    std::vector<benxdb::Column> columns;
    columns.emplace_back("x", benxdb::FLOAT);
    columns.emplace_back("y", benxdb::FLOAT);
    columns.emplace_back("z", benxdb::FLOAT);
    columns.emplace_back("intensity", benxdb::FLOAT);
    benxdb::Database db;
    db.CreateTable(std::make_shared<benxdb::Table>("test", columns));

    benxdb::PointCloud<benxdb::PointXYZI> point_cloud;
    for (int i = 0; i < 7000089; i++) {
        benxdb::PointXYZI point{};
        point.x = (float)(rand()%400000);
        point.y = (float)(rand()%400000);
        point.z = (float)(rand()%400000);
        point.intensity = 1.023f;
        point_cloud.Append(point);
    }
    std::cout << point_cloud.Size() << std::endl;
    db.AppendPointCloud("test", point_cloud);
}


TEST(DatabaseTest, QueryCircleTest) {
    auto db = std::make_shared<benxdb::Database>();
    benxdb::PointCloud<benxdb::PointXYZ> res;
    benxdb::RectangleQuery query("test");
    query.SetRectangle(40, 70, 200, 240);
    db->QueryPointCloud(query, res);
}

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc,argv);
    return RUN_ALL_TESTS();
}